package com.ruoyi.account.service.impl;

import com.ruoyi.account.service.IAccountMerchantInfoService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.db.domain.AccountMerchantInfo;
import com.ruoyi.db.mapper.AccountMerchantInfoMapper;
import com.ruoyi.db.mapper.MerchantBaseInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 商户账户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-28
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class AccountMerchantInfoServiceImpl implements IAccountMerchantInfoService {
    @Autowired
    private AccountMerchantInfoMapper accountMerchantInfoMapper;

    @Autowired
    private MerchantBaseInfoMapper merchantBaseInfoMapper;


    /**
     * 查询商户账户信息
     *
     * @param id 商户账户信息主键
     * @return 商户账户信息
     */
    @Override
    public AccountMerchantInfo selectAccountMerchantInfoById(Long id) {
        return accountMerchantInfoMapper.selectAccountMerchantInfoById(id);
    }

    /**
     * 查询商户账户信息列表
     *
     * @param accountMerchantInfo 商户账户信息
     * @return 商户账户信息
     */
    @Override
    public List<AccountMerchantInfo> selectAccountMerchantInfoList(AccountMerchantInfo accountMerchantInfo) {
        return accountMerchantInfoMapper.selectAccountMerchantInfoList(accountMerchantInfo);
    }

    /**
     * 查询商户账户信息列表
     *
     * @param accountMerchantInfo 商户账户信息
     * @return 商户账户信息
     */
    @Override
    public List<Map<String, Object>> selectAccountMerchantInfoMapList(AccountMerchantInfo accountMerchantInfo) {
        return accountMerchantInfoMapper.selectAccountMerchantInfoMapList(accountMerchantInfo);
    }

    @Override
    public List<Map<String, Object>> selectAccountMerchantInfoMap(AccountMerchantInfo accountMerchantInfo) {

        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            Long deptId = SecurityUtils.getDeptId();
            Long InfoId = merchantBaseInfoMapper.seletInfoIdBydeptId(deptId);
            accountMerchantInfo.setInfoId(InfoId);
        }
        return accountMerchantInfoMapper.selectAccountMerchantInfoMap(accountMerchantInfo);
    }

    /**
     * 新增商户账户信息
     *
     * @param accountMerchantInfo 商户账户信息
     * @return 结果
     */
    @Override
    public int insertAccountMerchantInfo(AccountMerchantInfo accountMerchantInfo) {
        accountMerchantInfo.setCreateTime(DateUtils.getNowDate());
        return accountMerchantInfoMapper.insertAccountMerchantInfo(accountMerchantInfo);
    }

    /**
     * 修改商户账户信息
     *
     * @param accountMerchantInfo 商户账户信息
     * @return 结果
     */
    @Override
    public int updateAccountMerchantInfo(AccountMerchantInfo accountMerchantInfo) {
        accountMerchantInfo.setUpdateTime(DateUtils.getNowDate());
        return accountMerchantInfoMapper.updateAccountMerchantInfo(accountMerchantInfo);
    }

    /**
     * 批量删除商户账户信息
     *
     * @param ids 需要删除的商户账户信息主键
     * @return 结果
     */
    @Override
    public int deleteAccountMerchantInfoByIds(Long[] ids) {
        return accountMerchantInfoMapper.deleteAccountMerchantInfoByIds(ids);
    }

    /**
     * 删除商户账户信息信息
     *
     * @param id 商户账户信息主键
     * @return 结果
     */
    @Override
    public int deleteAccountMerchantInfoById(Long id) {
        return accountMerchantInfoMapper.deleteAccountMerchantInfoById(id);
    }

    @Override
    public AccountMerchantInfo selectAccountMerchantInfoByInfoId(Long infoId) {
        return accountMerchantInfoMapper.selectAccountMerchantInfoByInfoId(infoId);
    }

    @Override
    public List<Map<String, String>> getSimpleMapListByOwnerId(Long userId) {
        return accountMerchantInfoMapper.getSimpleMapListByOwnerId(userId);
    }
}
